package com.idbk.chargestation.fragment;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.idbk.chargestation.R;
import com.idbk.chargestation.adapter.BookHistoryAdapter;
import com.idbk.chargestation.api.ChargeStationAPI;
import com.idbk.chargestation.base.BaseActivity;
import com.idbk.chargestation.bean.JsonMyBooking;
import com.idbk.chargestation.bean.JsonMyBookingItem;
import com.idbk.chargestation.net.EHttpResponse;
import com.idbk.chargestation.util.GsonUtils;
import com.idbk.chargestation.util.UIUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 预约历史界面
 *
 * @author zhx
 */
public class FragmentBookEnd extends Fragment implements OnClickListener {

    /**
     * 每次刷新回来的数据个数
     */
    public final static int PAGE_SIZE = 10;
    public View mView;
    private Context mContext;
    private PullToRefreshListView mListView;
    private int mPageIndex = 1;
    private StringRequest mRequest;
    private ProgressDialog mDialog;
    private List<JsonMyBookingItem> mData;
    private BookHistoryAdapter mAdapter;
    private LinearLayout mLayoutEmpty, mLayoutError;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        mView = inflater.inflate(R.layout.fragment_book_end, container, false);

        setupView();

        setupData();

        return mView;
    }

    public void setupView() {
        mContext = getActivity();
        mLayoutEmpty = (LinearLayout) mView.findViewById(R.id.tj_empty_view);
        mLayoutError = (LinearLayout) mView.findViewById(R.id.tj_error_view);
        mLayoutError.setOnClickListener(this);

        mData = new ArrayList<JsonMyBookingItem>();
        mAdapter = new BookHistoryAdapter(mContext, mData);
        mListView = (PullToRefreshListView) mView.findViewById(R.id.pullToRefreshListView);
        mListView.setMode(Mode.BOTH);
        mListView.setOnRefreshListener(new OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(
                    PullToRefreshBase<ListView> refreshView) {
//				下拉的时候数据重置				
                mRequest = ChargeStationAPI.getBespeakHistory(
                        1, PAGE_SIZE, mReloadResponse);
            }

            @Override
            public void onPullUpToRefresh(
                    PullToRefreshBase<ListView> refreshView) {
                //上拉的时候添加选项
                mRequest = ChargeStationAPI.getBespeakHistory(
                        mPageIndex + 1, PAGE_SIZE, mLoadMoreResponse);
            }
        });
        mListView.setAdapter(mAdapter);
    }

    public void setupData() {
        mDialog = ProgressDialog.show(mContext, null, "加载中...", true, true, new DialogInterface.OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                if (mRequest != null) {
                    mRequest.cancel();
                }
            }
        });
        mDialog.setCanceledOnTouchOutside(false);
        mPageIndex = 1;
        mRequest = ChargeStationAPI.getBespeakHistory(mPageIndex, PAGE_SIZE, mResponse);
    }

    private final EHttpResponse mResponse = new EHttpResponse() {

        @Override
        public void onEResponse(String response) {
            JsonMyBooking mResults = GsonUtils.toBean(JsonMyBooking.class, response);
            if (BaseActivity.handleResponseStatus(getActivity(), mResults)) {
                if (mResults.items.size() <= 0) {
                    UIUtil.showEmpty(mLayoutEmpty, mLayoutError);
                    mListView.setVisibility(View.GONE);

                } else {
                    UIUtil.hideAll(mLayoutEmpty, mLayoutError);
                    mListView.setVisibility(View.VISIBLE);
                    mData.clear();

                    for (JsonMyBookingItem items : mResults.items) {
                        if (items.bespeakStatue != 1) {
                            mData.add(items);
                        }
                    }
                    mAdapter.notifyDataSetChanged();
                }
            }
        }

        @Override
        public void onEErrorResponse(VolleyError error) {
            UIUtil.showNetError(mLayoutEmpty, mLayoutError);
            mListView.setVisibility(View.GONE);
        }

        @Override
        public void onEFinish() {
            super.onEFinish();
            mListView.onRefreshComplete();
            mDialog.dismiss();
        }
    };

    private final EHttpResponse mReloadResponse = new EHttpResponse() {

        @Override
        public void onEResponse(String response) {
            JsonMyBooking mResults = GsonUtils.toBean(JsonMyBooking.class, response);

            if (BaseActivity.handleResponseStatus(getActivity(), mResults)) {
                if (mResults.items != null && mResults.items.size() > 0) {
                    mData.clear();

                    for (JsonMyBookingItem items : mResults.items) {
                        if (items.bespeakStatue != 1) {
                            mData.add(items);
                        }
                    }
                    mListView.setVisibility(View.VISIBLE);
                    UIUtil.hideAll(mLayoutEmpty, mLayoutError);
                    mAdapter.notifyDataSetChanged();

                } else {
                    Toast.makeText(mContext, R.string.common_tip_rusult_nomoredata, Toast.LENGTH_SHORT).show();
                }
            }
            mDialog.dismiss();
        }

        @Override
        public void onEErrorResponse(VolleyError error) {
            UIUtil.showNetError(mLayoutEmpty, mLayoutError);
            mListView.setVisibility(View.GONE);
        }

        @Override
        public void onEFinish() {
            super.onEFinish();
            mListView.onRefreshComplete();
            mDialog.dismiss();
        }
    };


    private final EHttpResponse mLoadMoreResponse = new EHttpResponse() {

        @Override
        public void onEResponse(String response) {
            JsonMyBooking mResults = GsonUtils.toBean(JsonMyBooking.class, response);

            if (BaseActivity.handleResponseStatus(getActivity(), mResults)) {
                if (mResults.items != null && mResults.items.size() > 0) {
                    mPageIndex++;

                    for (JsonMyBookingItem items : mResults.items) {
                        if (items.bespeakStatue != 1) {
                            mData.add(items);
                        }
                    }
                    mListView.setVisibility(View.VISIBLE);
                    UIUtil.hideAll(mLayoutEmpty, mLayoutError);
                    mAdapter.notifyDataSetChanged();
                } else {
                    Toast.makeText(mContext, R.string.common_tip_rusult_nomoredata, Toast.LENGTH_SHORT).show();
                }
            }
            mDialog.dismiss();
        }

        @Override
        public void onEErrorResponse(VolleyError error) {
            UIUtil.showNetError(mLayoutEmpty, mLayoutError);
            mListView.setVisibility(View.GONE);
        }

        @Override
        public void onEFinish() {
            super.onEFinish();
            mListView.onRefreshComplete();
            mDialog.dismiss();
        }
    };


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tj_error_view:
                setupData();
                break;
            default:
                break;
        }
    }


}
